var common = require('../../common');
var pool = require('../index');
var mysql = require('mysql');
var YjxBaseModel = require('./YjxBaseModel');
var CONFIG = require('../../config');


var YjxActStatsModel = function (obj) {
    YjxBaseModel.call(this,obj);

//`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
//`act_id` bigint(20) NOT NULL COMMENT '活动id',
//`view_cnt` bigint(10) NOT NULL DEFAULT '0' COMMENT '实际查看次数',
//`show_view_cnt` bigint(10) NOT NULL DEFAULT '0' COMMENT '显示查看次数',
//create_time` datetime NOT NULL COMMENT '创建时间',
//`update_time` datetime NOT NULL COMMENT '更新时间',
//`update_by` varchar(50) DEFAULT NULL COMMENT '更新者',
//`del_flg` int(1) NOT NULL DEFAULT '0' COMMENT '逻辑删除标记',
//PRIMARY KEY (`id`),
//KEY `IDX_ACT_STATS_01` (`act_id`)

};

YjxActStatsModel.prototype = new YjxBaseModel();

// 取得表名
YjxActStatsModel.prototype.getTableName = function(){
    return 'yjx_act_stats';
};
// 取得查询语句中where 后面匹配主键的条件（需要各派生类进行重载）
YjxActStatsModel.prototype.getWhereConditionOfKeyEqual = function(){
    return 'id='+this.id;
};

/**
 * 更新活动的访问次数
 *
 * @author 毛凯
 * @date 2018-6-14
 *
 * @param actId  活动id
 * @param count  访问次数
 * @param callback 回调
 *           err: 查询失败
 *           results: 活动列表
 *
 * */
YjxActStatsModel.prototype.updActivityViewStates = function (actId,count,callback){
    pool.pool.getConnection(function (getConErr, connection) {
        if (getConErr) {
            console.log('YjxActStatsModel.js [updActivityViewStates] getConErr' + getConErr);
            return callback(getConErr, null);
        }
        var sql = "	UPDATE	" +
                "	  yjx.yjx_act_stats yas	" +
                "	SET	" +
                "	  yas.view_cnt = "+count+",	" +
                "	  yas.update_time = NOW()	" +
                "	WHERE yas.act_id = "+actId+" " +
                "	AND yas.del_flg = 0	" ;
        console.log(sql);
        connection.query(sql, function (queryErr, results) {
            connection.release();
            if (queryErr) {
                console.log('YjxActivityModel.js [getActivityListCnt] queryErr' + queryErr)
            }
            return callback(queryErr, results);
        });
    })
};


/**
 * 查询活动状态
 *
 * @author 毛凯
 * @date 2018-6-14
 *
 * @param actId  活动id
 * @param callback 回调
 *           err: 查询失败
 *           results: 活动列表
 *
 * */
YjxActStatsModel.prototype.checkActivityStateInfo = function (actId,callback){
    pool.pool.getConnection(function (getConErr, connection) {
        if (getConErr) {
            console.log('YjxActStatsModel.js [updActivityViewStates] getConErr' + getConErr);
            return callback(getConErr, null);
        }
        var sql = "SELECT COUNT(yas.id) as `count` FROM yjx.yjx_act_stats yas WHERE yas.act_id = "+actId+"  AND yas.del_flg = 0";
        console.log(sql);
        connection.query(sql, function (queryErr, results) {
            connection.release();
            if (queryErr) {
                console.log('YjxActivityModel.js [getActivityListCnt] queryErr' + queryErr)
            }
            return callback(queryErr, results[0].count);
        });
    })
};


/**
 * 添加活动状态
 *
 * @author 毛凯
 * @date 2018-6-14
 *
 * @param actId  活动id
 * @param count  访问次数
 * @param callback 回调
 *           err: 查询失败
 *           results: 活动列表
 *
 * */
YjxActStatsModel.prototype.addActivityStateInfo = function (actId,count,callback){
    pool.pool.getConnection(function (getConErr, connection) {
        if (getConErr) {
            console.log('YjxActStatsModel.js [updActivityViewStates] getConErr' + getConErr);
            return callback(getConErr, null);
        }
        var sql = "	INSERT INTO yjx.yjx_act_stats(	" +
            "	  act_id,	" +
            "	  view_cnt,	" +
            "	  create_time,	" +
            "	  update_time,	" +
            "	  del_flg	" +
            "	)	" +
            "	VALUES	" +
            "	  (	" +
            "	    '"+actId+"',	" +
            "	    '"+count+"',	" +
            "	    now(),	" +
            "	    now(),	" +
            "	    0	" +
            "	  )	" ;

        console.log(sql);
        connection.query(sql, function (queryErr, results) {
            connection.release();
            if (queryErr) {
                console.log('YjxActivityModel.js [getActivityListCnt] queryErr' + queryErr)
            }
            return callback(queryErr, results);
        });
    })
};



module.exports = YjxActStatsModel;

